<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Domain extends Model
{
    use SoftDeletes;

    const STATUSES = [
        0 => '禁用',
        1 => '启用',
    ];

    protected $fillable = ['domain', 'status'];

    public function getStatusWordAttribute()
    {
        if (array_key_exists($this->status, self::STATUSES)) {
            return self::STATUSES[$this->status];
        }
        return '未知';
    }

    /**
     * 随机获取一个域名
     * @return mixed
     */
    public static function one()
    {
        $arr = static::query()
            ->where('status', 1)
            ->get()
            ->toArray();
        if (empty($arr)) {
            die('<!DOCTYPE html><html><head><meta charset="UTF-8"><title>没有配置备用域名</title></head><body><p>没有配置备用域名</p></body></html>');
        }
        shuffle($arr);
        return array_pop($arr)['domain'];
    }
}
